package com.lbloca.locate.ui.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.gyf.immersionbar.ImmersionBar;
import com.lbloca.locate.R;
import com.lbloca.locate.application.LoApplication;
import com.lbloca.locate.biz.AppBiz;
import com.lbloca.locate.biz.UserBiz;
import com.lbloca.locate.ui.base.BaseActivity;
import com.lbloca.locate.utils.AppUtils;
import com.lbloca.locate.utils.PermissionUtils;
import com.lbloca.locate.utils.ToastUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class BindMobileActivity extends BaseActivity {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_et_mobile)
    EditText mEtMobile;
    @BindView(R.id.id_go_bind)
    TextView mTvGoBind;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @OnClick({R.id.id_go_bind})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_go_bind:
                String mobile = mEtMobile.getText().toString();
                if(!TextUtils.isEmpty(mobile)&&isPhoneNumber(mobile)){
                    register(mobile);
                    BindMobileActivity.this.finish();
                }else{
                    ToastUtils.showShort("请输入正确的手机号码");
                }
                break;
            default:
                break;
        }
    }

    private void register(String phone) {
        AppBiz.getInstance().register(phone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showShort("绑定失败");
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("response", response + "");
                if (!TextUtils.isEmpty(response) && response.contains("id")) {
                    UserBiz.getInstance().setUserInfo(response);
                    ToastUtils.showShort("绑定成功");
                }else{
                    ToastUtils.showShort("绑定失败");
                }
            }
        });
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, BindMobileActivity.class);
        context.startActivity(intent);
    }

    public static boolean isPhoneNumber(String input) {// 判断手机号码是否规则
        String regex = "(1[0-9][0-9]|15[0-9]|18[0-9])\\d{8}";
        Pattern p = Pattern.compile(regex);
        return p.matches(regex, input);//如果不是号码，则返回false，是号码则返回true
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .titleBar(mToolbar)
                .init();
    }
}
